package com.zd.dao;

import com.zd.entity.News;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NewsDao extends PagingAndSortingRepository<News,Long> {
    /*
    * 根据tname查询主题下新闻
    * @param tname
    * @return
    * */
    public List<News> getAllByOrderByNcreateDateDesc();

    @Query(value = "select * from news where ntid = (select tid from topic where tname= ?1) order by ncreateDate desc limit 5",nativeQuery = true)
    public List<News> getAllNewsByTname(@Param("tname") String tname);

    /*
     * 根据主题名查询主题下新闻
     * @param ntid
     * @return
     * */
    public List<News> getAllByNtidOrderByNcreateDateDesc(Long ntid);
    /*
    * 查询指定新闻详细信息
    * */
    News getNewsByNid(Long nid);
    /*
    * 查询所有新闻条数
    * */
    int countAllBy();

    @Query(value = "select * from news order by ncreateDate desc limit ?1,?2",nativeQuery = true)
    List<News> getNewsByPage(@Param("index") int index,@Param("pageSize") int pageSize);
    //判断某个主题下是否存在新闻
    boolean existsNewsByNtid(Long ntid);
    //删除指定新闻
    void deleteByNid(Long nid);
}
